package admission.ecnu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.tagext.TryCatchFinally;

import admission.ecnu.db.DBConnection;
import admission.ecnu.object.OResult;
import admission.ecnu.po.User;

public class ResultDAO {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	
	public ResultDAO() {
		// TODO Auto-generated constructor stub
		this.conn = DBConnection.getConnection();
	}
	
	public List<OResult> resultSelect(){
		List<OResult> list = new ArrayList<OResult>();
		try {
			this.pstmt = this.conn.prepareStatement("select * from test_info");
			this.rs = this.pstmt.executeQuery();
			while(this.rs.next()){
				OResult examResult = new OResult();
				examResult.setCourse_id(rs.getString("course_id"));
				examResult.setCourse_name(rs.getString("course_name"));
				examResult.setTest_time(rs.getString("test_time"));
				examResult.setClassroom(rs.getString("classroom"));
				examResult.setChief_teacher(rs.getString("chief_teacher"));
				examResult.setVice_teacher01(rs.getString("vice_teacher01"));
				examResult.setVice_teacher02(rs.getString("vice_teacher02"));
				examResult.setPatrol_teacher(rs.getString("patrol_teacher"));
				examResult.setBackup_teacher(rs.getString("backup_teacher"));
				list.add(examResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.rs.close();
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return list;
	}
	public int resultDelete(){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("delete from test_info");
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
	public int resultInsert(OResult examResult){
		int i = 0;
		try {
			this.pstmt = this.conn.prepareStatement("insert into test_info(course_id,course_name,test_time,classroom,chief_teacher,vice_teacher01, vice_teacher02, patrol_teacher, backup_teacher) value(?,?,?,?,?,?,?,?,?)");
			this.pstmt.setString(1,examResult.getCourse_id());
			this.pstmt.setString(2,examResult.getCourse_name());
			this.pstmt.setString(3,examResult.getTest_time());
			this.pstmt.setString(4,examResult.getClassroom());
			this.pstmt.setString(5,examResult.getChief_teacher());
			this.pstmt.setString(6,examResult.getVice_teacher01());
			this.pstmt.setString(7,examResult.getVice_teacher02());
			this.pstmt.setString(8,examResult.getPatrol_teacher());
			this.pstmt.setString(9,examResult.getBackup_teacher());
			
			i = this.pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				this.pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.closeConnection(this.conn);
		}
		return i;
	}
}
